public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;
        TreeNode(int key) {
            this.key = key;
        }
    }
    public TreeNode root;
    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);//将要插入的值初始化成一个节点
        if(root == null){//处理树中没有元素的情况
            root = node;
            return true;
        }
        TreeNode cur = root;//将cur从root开始遍历
        TreeNode prev = null;//用来记录cur的父亲节点
        while(cur != null){
            prev = cur;
            if(cur.key < key){//比根节点大，往右子树移动
                cur = cur.right;
            }else if(cur.key > key){//比根节点小，往左子树移动
                cur = cur.left;
            }else{//判断是否已经含有该节点
                System.out.println("已经有该元素！");
                return false;
            }
        }
        if(prev.key < key){//判断将key插入到该节点的左边或者右边
            prev.right = node;
        }
        if(prev.key > key){
            prev.left = node;
        }
        return true;
    }
    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode cur = root;
        while(cur != null){
            if(cur.key < key){
                cur = cur.right;
            }else if(cur.key > key){
                cur = cur.left;
            }else {
                return cur;
            }
        }
        return null;
    }
    //删除key的值
    public boolean remove(int key) {
        TreeNode cur = root;
        TreeNode prev = null;
        while(cur != null){
            if(cur.key < key){
                prev = cur;
                cur = cur.right;
            }else if(cur.key > key){
                prev = cur;
                cur = cur.left;
            }else {
                removeTreeNode1(prev,cur);
                return true;
            }
        }
        return false;
    }
    private void removeTreeNode(TreeNode prev, TreeNode cur){
        if(cur.left == null){
            if(cur == root){
                root = cur.right;
            } else if(prev.right == cur){
                prev.right = cur.right;
            }else if(prev.left == cur){
                prev.left = cur.right;
            }
        }else if(cur.right == null){
            if(cur == root){
                root = cur.left;
            }else if(prev.right == cur){
                prev.right = cur.left;
            }else if(prev.left == cur){
                prev.left = cur.left;
            }
        }else {
            TreeNode tmpPrev = cur;
            TreeNode tmpCur = cur.right;
            while(tmpCur.left != null){
                tmpPrev = tmpCur;
                tmpCur = tmpCur.left;
            }
            cur.key = tmpCur.key;
            if(tmpCur == tmpPrev.right){
                tmpPrev.right = tmpCur.right;
            }else{
                tmpPrev.left = tmpCur.right;
            }
        }
    }
    private void removeTreeNode1(TreeNode parent, TreeNode cur){
        if(cur.left == null){
            if(cur == root){
                root = cur.right;
            } else if(parent.right == cur){
                parent.right = cur.right;
            }else if(parent.left == cur){
                parent.left = cur.right;
            }
        }else if(cur.right == null){
            if(cur == root){
                root = cur.left;
            }else if(parent.right == cur){
                parent.right = cur.left;
            }else if(parent.left == cur){
                parent.left = cur.left;
            }
        }else {
            TreeNode tmpParent = cur;
            TreeNode tmpCur = cur.left;
            while(tmpCur.right != null){
                tmpParent = tmpCur;
                tmpCur = tmpCur.right;
            }
            cur.key = tmpCur.key;
            if(tmpCur == tmpParent.left){
                tmpParent.left = tmpCur.left;
            }else{
                tmpParent.right = tmpCur.left;
            }
        }
    }

    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insert(3);
        binarySearchTree.insert(4);
        binarySearchTree.insert(2);
        binarySearchTree.insert(6);
        binarySearchTree.remove(3);
        System.out.println();
    }
}